package com.osdp.auth.entity;

import lombok.Data;
import org.apache.commons.compress.utils.Sets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@Data
public class User implements UserDetails {
    private String id;
    private String password;
    private String username;
    private String realName;
    private String salt;
    private String tenantId;
    private List<String> tenantIds;
    private boolean isSuperAdmin;
    //当前权限
    private List<String> roleCodes;

    //1.List:可重复,有序,数组或者链表存储;2.Set:不可重复,无序,使用map存储
//    private Set<String> authorities = Sets.newHashSet();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        for(String role : roleCodes) {
            if(StringUtils.isEmpty(role)) {continue;}
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);
            authorities.add(authority);
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
